package com.bsoft.ecommerce.service.async;

import com.bsoft.ecommerce.constant.AsyncTaskStatusEnum;
import com.bsoft.ecommerce.goods.GoodsInfo;
import com.bsoft.ecommerce.vo.AsyncTaskInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

//异步管理器
@Slf4j
@Component
public class AsynTaskManager {
    //异步任务执行信息
    private final Map<String , AsyncTaskInfo> taskInfoMap = new HashMap<>(16);
    @Autowired
    private IAsynService asynService;
   //初始化异步任务
    public AsyncTaskInfo initTask(){
        AsyncTaskInfo taskInfo= new AsyncTaskInfo();
         taskInfo.setTaskId(UUID.randomUUID().toString());
         taskInfo.setStatus(AsyncTaskStatusEnum.STARTED);
         taskInfo.setStartTime(new Date());
         taskInfoMap.put(taskInfo.getTaskId(),taskInfo);
         return taskInfo;
    }
    //提交异步任务
    public AsyncTaskInfo submit(List<GoodsInfo> goodsInfos){
          AsyncTaskInfo taskInfo= initTask();
          asynService.asynImportGoods(goodsInfos,taskInfo.getTaskId());
          return  taskInfo;
    }
    public  void setTaskInfo(AsyncTaskInfo taskInfo){
        taskInfoMap.put(taskInfo.getTaskId(),taskInfo);
    }
    public  AsyncTaskInfo getTaskInfo(String taskId){
        return  taskInfoMap.get(taskId);
    }
}
